<?php
declare(strict_types=1);
 
namespace App\JsonRpc\ServiceProvider;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Service\CouponService as Coupon;
use App\Service\OilChannelService;
use App\Utils\ApiUtils;
use App\Validate\ChannelRpcValidation;
use App\Validate\CouponValidation;
use App\Validate\PayFullVerifyRpcValidation;
use Hyperf\RpcServer\Annotation\RpcService;
use App\Utils\ServerUtil;
use Hyperf\Utils\ApplicationContext;

/**
 * 注意，如希望通过服务中心来管理服务，需在注解内增加 publishTo 属性
 * @RpcService(name="PayFullVerifyService", protocol="jsonrpc", server="jsonrpc")
 */
class PayFullVerifyService implements PayFullVerifyServiceInterface
{
    public function consume($data): string{
        $data = json_decode($data,true);
        ServerUtil::info('rpc-PayFullVerifyService-进入', ['path'=>'PayFullVerifyService', 'parameter'=>$data]);
        ApplicationContext::getContainer()->get(PayFullVerifyRpcValidation::class)->check($data, 'payFullVerify')->response();

        //渠道检验
        if(empty($data['channel']['type'])){
            throw new BusinessException(ErrorCode::ERR_BUESSUS, 'type不存在');
        }

        $obj = ApplicationContext::getContainer()->get(\App\Service\ChannelService::class);
        $channelValidation = ApplicationContext::getContainer()->get(ChannelRpcValidation::class);

        switch ($data['channel']['type'])
        {
            case 1:
                //获取渠道信息
                $channelValidation->check($data['channel'], 'channel_amount')->response();;
                $res = $obj->amountByChannelInfo($data['channel']['recharge_type'], $data['channel']['operator_type'], $data['channel']['city_id'], $data['channel']['amount']);
                if(empty($res)){
                    $responseData = ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用渠道');
                }else{
                    $responseData = ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res);
                }
                break;
            case 2:
                //根据商户号获取渠道信息
                $channelValidation->check($data['channel'], 'channel_merchant_no')->response();
                $res = $obj->merchantNoByChannelInfo($data['channel']['merchant_no']);
                if($res){
                    $responseData = ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res);
                }else{
                    $responseData = ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用渠道');
                }
                break;
            case 3:
                //根据商户号获取合作商信息
                $channelValidation->check($data['channel'], 'channel_merchant_no')->response();
                $res = ApplicationContext::getContainer()->get(OilChannelService::class)->merchantNoByChannelInfo($data['channel']['merchant_no']);
                if($res){
                    $responseData = ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res);
                }else{
                    $responseData = ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用合作商');
                }
                break;
            case 4:
                //根据充值面值获取合作商信息
                $channelValidation->check($data['channel'], 'channel_amount_oli')->response();
                $res = ApplicationContext::getContainer()->get(OilChannelService::class)->amountByChannelInfo($data['channel']['ori_amount'], $data['channel']['amount'], $data['channel']['recharge_type']);
                if($res){
                    $responseData = ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res);
                }else{
                    $responseData = ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用合作商');
                }
                break;
            default:
                $responseData = ApiUtils::send(ErrorCode::ERR_BUESSUS,'type解析失败');
        }
        ServerUtil::info('rpc-ChannelService-退出', ['path'=>'ChannelService','param'=>$data['channel'],'res'=>$responseData]);

        if($responseData['errCode'] != ErrorCode::SUCCESS){
            throw new BusinessException(ErrorCode::ERR_SERVER, $responseData['msg']);
        }

        //支付时没有使用优惠券
        if(empty($data['coupon'])){
            $responseData = json_encode(ApiUtils::send(ErrorCode::SUCCESS,'检验通过'));
            ServerUtil::info('rpc-PayFullVerifyService-退出', ['path'=>'PayFullVerifyService', 'parameter'=>$data,'res'=>$responseData]);
            return $responseData;
        }

        //优惠劵校验
        if(empty($data['coupon']['type'])){
            throw new BusinessException(ErrorCode::ERR_BUESSUS, 'type不存在');
        }

        make(CouponValidation::class)->check($data['coupon'], 'rpcCouponService')->response();
        $obj =  ApplicationContext::getContainer()->get(Coupon::class);
        switch ($data['coupon']['type'])
        {
            case 1:
                //返回可用红包信息
                $res = $obj->getCouponByCondition($data['coupon']['openid'],$data['coupon']['coupon_id'],2);
                ServerUtil::info('rpc-CouponService-根据条件获取可用红包信息',['param'=>$data['coupon'],'res'=>$res]);
                $responseData=json_encode(ApiUtils::send($res['code'],$res['msg'],$res['data']));
                break;
            case 2:
                //返回不限状态红包信息
                $res = $obj->getCouponByCondition($data['coupon']['openid'],$data['coupon']['coupon_id']);
                ServerUtil::info('rpc-CouponService-根据条件获取红包信息',['param'=>$data['coupon'],'res'=>$res]);
                $responseData=json_encode(ApiUtils::send($res['code'],$res['msg'],$res['data']));
                break;
            default:
                $responseData=json_encode(ApiUtils::send(ErrorCode::ERR_SERVER,'type解析失败'));
        }

        ServerUtil::info('rpc-PayFullVerifyService-退出', ['path'=>'PayFullVerifyService', 'parameter'=>$data,'res'=>$responseData]);
        return $responseData;
    }
}
